<template>
  <div>
    <el-dialog
      title="编辑账号"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-form
        label-position="right"
        label-width="120px"
        :model="formLabelAlign"
      >
        <el-form-item label="账号：">
          <el-input v-model="formLabelAlign.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="modifyChange">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";
import axios from "axios";
export default {
  components: {},
  props: {},
  data() {
    return {
      dialogVisible: false,
      formLabelAlign: {
        name: "",
      },
    };
  },
  computed: {
    ...mapState({}),
  },
  watch: {},
  beforeDestroy() {},
  created() {},
  methods: {
    handleClose() {
      this.dialogVisible = false;
      this.$message({
        type: "warning",
        message: "已取消编辑",
      });
    },
    openDialog() {
      this.dialogVisible = true;
    },
    submit() {
      console.log(this.formLabelAlign);
    },
    modifyChange: function () {
      let oldid = localStorage.getItem("yh");
      let newid = this.formLabelAlign.name;
      let ind = localStorage.getItem("lx");

      console.log(newid, oldid);
      if (ind == "1") {
        axios
          .post("/api/user/updateteaaccount", { newid, oldid })
          .then((response) => {
            console.log(response.data);
            // location.reload();
            this.dialogVisible = false;
            localStorage.setItem("yh", newid);
            axios.post("/api/user/showtabletea", {}).then((response) => {
              //这里进行过滤，只保留当前登陆学生的信息，其他过滤
              //filter 返回true表示保留该元素（通过测试），false则不保留
              var newArr = response.data.filter((item) => {
                if (newid === item.teacherid) {
                  return true;
                }
              });
              this.$emit("changeAccount", newArr);
            });
            // this.$router.push("/");
          })
          .catch(function (error) {
            console.log(error);
          });
      } else if (ind == "2") {
        axios
          .post("/api/user/updatestuaccount", { newid, oldid })
          .then((response) => {
            console.log(response.data);
            this.dialogVisible = false;
            localStorage.setItem("yh", newid);
            // location.reload();
            axios.post("/api/user/showtable", {}).then((response) => {
              //这里进行过滤，只保留当前登陆学生的信息，其他过滤
              //filter 返回true表示保留该元素（通过测试），false则不保留
              var newArr = response.data.filter((item) => {
                if (newid === item.studentid) {
                  return true;
                }
              });
              this.$emit("changeAccount", newArr);
            });
            axios
              .post("/api/user/updaterecordaccount", { newid, oldid })
              .then((res) => {
                console.log(res);
              });
          })
          .catch(function (error) {
            console.log(error);
          });
      }
      //   let user_id = localStorage.getItem("yh");
      //   let user_info = this.formLabelAlign;

      //   console.log(user_id);
      //   console.log(user_info);
      //   axios
      //     .post("/api/user/change_stuaccount", { user_id, user_info })
      //     .then(function (response) {
      //       console.log(response.data);
      //       // location.reload();
      //     })
      //     .catch(function (error) {
      //       console.log(error);
      //     });
      //   // $("#changeInfo").modal("hide");
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-button {
  position: static !important;
}
/deep/ .el-dialog {
  .el-dialog__header {
    background: #143853 !important;
  }
  .el-dialog__title {
    color: #ffffff;
  }
  .el-dialog__body {
    color: #ffffff;
    padding-bottom: 20px;
    background: #0f2d43 !important;
  }
  .el-dialog__footer {
    padding-right: 40px;
    background: #0f2d43;
  }
  .el-form-item__label {
    color: #ffffff;
  }
  .el-input__inner {
    background: #0355a4 !important;
    border: none;
    color: #ffffff;
  }
}
</style>